package sg.edu.nus.iss.cats.plugin;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

import sg.edu.nus.iss.cats.dataaccess.DAOFactory;

public class DBAccessPlugin implements PlugIn{   
    
    private String accessType;  
    private ServletContext ctxt;
    
    public DBAccessPlugin() {  }
    
    //Value read from Struts config file
    public void setAccessType(String accessType){    
        this.accessType = accessType;
        
    }   
    public void init(ActionServlet servlet, ModuleConfig config) throws ServletException{    
        
        if ((accessType==null) || (accessType.length() == 0)){
            throw new ServletException("You must specify accessType: ");}
        
        ctxt = servlet.getServletContext();
        try {
            //Create the specific factory depending on config
          
          int accessConfig=1;
          
          if (accessType.equals("HIB")) {
               accessConfig=2;
          }
          DAOFactory.setAccessTypeConfig(accessConfig);            
          DAOFactory.setCtxt(ctxt);
        
        }catch (Exception e) {
            throw new ServletException("Unable to load PMF: " + e.getMessage());
        }
    }
    public void destroy(){   
   
        this.ctxt = null;
    }
}